#include "class_2.hh"
using namespace std;
Point::Point(int x,int y)
    :_pt_x(x)
    ,_pt_y(y)
    ,_name(new char[]{})
    {
        strcpy(_name,"one point");
        cout<<"point1"<<endl;
    }
void Point::gets(){
    cout<<_pt_x<<","<<_pt_y<<"!!"<<_name<<endl;
}
//析构
Point::~Point(){
    if(_name){
        delete [] _name;
        _name=nullptr;

    }
    cout<<"~Point()"<<endl;
}

//运算符重载
Point & Point::operator=(const Point &po){
    cout<<"operator="<<endl;
    _pt_x = po._pt_x;
    _pt_y = po._pt_y;
    //首先需要把原先自己的删除(this._name) 
    //再创建新空间 解决doublefree的问题
    if(this==&po){   //地址一样说明是同一个，是自赋值
        cout<<"self"<<endl;
        return *this;
    }
    if(_name){
        delete [] _name;
    }
    _name = new char[strlen(po._name)+1]();
    strcpy(_name,po._name);

    return *this; 
}
//
Point::Point(Point& point)
    :_pt_x(point._pt_x)
     ,_pt_y(point._pt_y)
     ,_name(new char[strlen(point._name)+1])
    {
        strcpy(_name,point._name);
        cout<<"cope"<<endl;
    }

int main()
{
    
    Point po(3,5);
    
    po.gets();
    Point p1(9,8);
    //赋值运算符重载
    po = p1;
    //自赋值
    p1 = p1;
    po.gets();
   
    Point p2 = po;
    p2.gets(); 
    // p1.~Point(); 最好不要自己调用
    return 0;
}

